import Dingo.API as api
import Dingo.sound as sound
import random

__author__ = "Sarah Mount"

PATH = '/tmp/sensor/par-test/' ### NOT PORTABLE!!!

class TheSimulation(api.Simulation):
	def __init__(self, graph):
		ether = api.Ether(self)
		api.Ether.RF_radius = 250
		sound.Beep.radius = 250
		sensors = [SendSensor(graph, ether), RecvSensor(graph, ether)]
		api.Simulation.__init__(self, sensors, graph)
		return
	def run(self):
		super(TheSimulation, self).run()

class SendSensor(api.Sensor, sound.SoundSensor):
	def __init__(self, graph, ether):
		id = 'SendSensor'
		x = random.randrange(150)
		y = random.randrange(150)
		col = api.SensorColor.White
		sched = api.Scheduler([SendTask(0, 0, self)])
		api.Sensor.__init__(self, graph, id, (x,y), col, sched, ether)
		sound.SoundSensor.__init__(self, id, PATH) 
		return

class RecvSensor(api.Sensor, sound.SoundSensor):
	def __init__(self, graph, ether):
		id = "RecvSensor"
		x = random.randrange(250)
		y = random.randrange(250)
		col = api.SensorColor.Grey
		sched = api.Scheduler([RecvTask(0, 0, self)])
		api.Sensor.__init__(self, graph, id, (x,y), col, sched, ether)
		sound.SoundSensor.__init__(self, id, PATH)
		return

class ColourBroadcastPacket(api.Packet):
	"""Defines packets to be sent across the network.
	This one just sends sensor colours. When a sensor receives
	a message, it should change colour according to the message
	it's received.
	"""
	def __init__(self, sender, msg):
		### sending node, receiving node, message_object
		### NOTE that we haven't given a receiving object,
		### because these packets will be broadcast
		api.Packet.__init__(self, sender, None, msg)
		return

class SendTask(api.Task):
	"""Send sounds and RF signals simultaneously."""
	def __init__(self, priority, repetition, sensor):
		api.Task.__init__(self, priority, repetition, sensor)
		return
	def task(self):
		rgb = random.randrange(1<<24)
		colour = api.SensorColor.FromRGBCombined(rgb)
		packet = ColourBroadcastPacket(self.sensor.id, colour)
		self.sensor.node.SetNodeColor(api.SensorColor.Green)
		### Send sound & RF simultaneously
		### self.sensor.par((method-obj_1, args_1), ...)
		self.sensor.par((self.sensor.make_sound, (api.Sensor.CLK_DUMMY,)),(self.sensor.broadcast, (api.SensorColor.Green, api.Sensor.CLK_DUMMY, packet)))
		## Sent!
		self.sensor.node.SetNodeColor(api.SensorColor.White)
		api.Sleep(random.uniform(1, 2))
		return

class RecvTask(api.Task):
	"""Process sounds and RF signals."""
	def __init__(self, priority, repetition, sensor):
		api.Task.__init__(self, priority, repetition, sensor)
		return
	def task(self):
		self.sensor.node.SetNodeColor(api.SensorColor.White)
		# Move sounds from probe file into sound_q
		self.sensor.read_from_microphone()
		# Process sounds in sound_q
		while not self.sensor.sound_q.empty():
			obj = self.sensor.sound_q.get()
			print self.sensor.id, "heard", obj.__repr__()
		# Process RF packets
		while not self.sensor.recv_q.empty():
			msg = self.sensor.recv_q.get()
			print_info = (self.sensor.id, msg.sender_id, msg.time_sent, msg.rssi)
			print "%s received packet from %s sent at %.3f with RSSI %f" % print_info
			self.sensor.node.SetNodeColour(msg.msg)
		print
		api.Sleep(random.uniform(1, 2))
		return
